<template>
  <gc-dialog
    :title="isEdit ? '编辑资源' : '新增资源'"
    :dialog-visible="true"
    @cancel="handleCancle"
    @ok="handleConfirm"
  >
    <el-form
      ref="formRef"
      label-position="top"
      :model="form"
      :rules="formRules"
    >
      <el-form-item label="资源名称" prop="name">
        <el-input type="text" v-model="form.name"  placeholder="请输入" />
      </el-form-item>
      <el-form-item label="上级资源" prop="parentId">
        <el-tree-select
          clearable
          v-model="form.parentId"
          node-key="id"
          check-strictly
          :data="parentResourceTree"
          :render-after-expand="true"
          :props="{ label: 'name' }"
          :default-expand-all="false"
        />
      </el-form-item>
      <el-form-item label="资源code编码" prop="code">
        <el-input type="text" v-model="form.code" placeholder="请输入" />
      </el-form-item>
      <el-form-item label="资源类型" prop="type">
        <el-select v-model="form.type" placeholder="请选择">
          <el-option value="menu" label="菜单"></el-option>
          <el-option value="btn" label="按钮"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="权重" prop="sort">
        <el-input-number
          class="input-number-align-left"
          style="width: 100%"
          step-strictly
          :step="1"
          :min="1"
          v-model="form.sort"
          placeholder="请输入资源权重（排序依据）" />
      </el-form-item>
    </el-form>
  </gc-dialog>
</template>

<script setup lang="ts" name="OperateResourceModal">
import { ref, reactive, computed, watch, type PropType } from 'vue'
import { ruleRequired, ruleMaxLength, IS_FILED_CODE, NAME_CN } from '@/utils/rules'
import type { FormInstance, FormRules } from 'element-plus'
import { apiCreateResource, apiUpdateResource, apiGetResourceTreeList } from '@/http/service'
import { ElMessage } from 'element-plus'
import type { ResourceTree } from '@/http/type'

const props = defineProps({
  resourceInfo: {
    type: Object as PropType<null | ResourceTree>,
    default: () => null
  }
})
const emit = defineEmits(['close-modal'])
const formRef = ref<null | FormInstance>(null)
const form = reactive({
  name: '',
  parentId: null,
  code: '',
  type: '',
  sort: null
})
const formRules = reactive<FormRules>({
  name: [ruleRequired('请输入资源名称'), ruleMaxLength(64), NAME_CN],
  code: [ruleRequired('请输入资源code编码'), ruleMaxLength(64), IS_FILED_CODE],
  type: [ruleRequired('请选择资源类型')]
})
const isEdit = computed(() => {
  return Boolean(props.resourceInfo)
})
const parentResourceTree = ref<Array<ResourceTree>>([])

/**
 * 查询所有类型为菜单类型的资源作为上级资源
 */
async function findMenuResourceTree () {
  const data = await apiGetResourceTreeList<Array<ResourceTree>>({ type: 'menu' })
  parentResourceTree.value = data
}
findMenuResourceTree()

async function handleConfirm () {
  const valid = await formRef.value?.validate()
  if (valid) {
    const API = isEdit.value ? apiUpdateResource : apiCreateResource
    await API(form)
    ElMessage.success(isEdit.value ? '编辑资源成功' : '新增资源成功')
    emit('close-modal', true)
  }
}

function handleCancle () {
  emit('close-modal')
}

watch(() => props.resourceInfo, val => {
  if (val) {
    const { id, name, parentId, sort, code, type } = val
    Object.assign(form, {
      id, name, parentId, sort, code, type
    })
  }
}, { immediate: true })
</script>

<style scoped lang="scss"></style>
